package com.example.lanouhn.bantangapp.activities.fragments;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.activities.PublishActivity;
import com.example.lanouhn.bantangapp.activities.adapters.VpSelectedAdapter;
import com.yongchun.library.view.ImageSelectorActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/22.
 */
public class Fragment_Communtiy extends Fragment {

    private static final int RESULT_OK = 200;
    private VpSelectedAdapter VPA;
    private ViewPager vp;
    private TabLayout tablayout;
    private List<String> tabNames;
    //viewpager的数据集合
    private List<Fragment> fragments;
    private ImageView community_title_bar_publish_topic;//编辑
    private ImageView comera;//相机

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.community_fragment, container, false);
        vp = (ViewPager) view.findViewById(R.id.vp);
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        community_title_bar_publish_topic = (ImageView) view.findViewById(R.id.community_title_bar_publish_topic);
        comera = (ImageView) view.findViewById(R.id.comera);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();//添加数据
        setDataToView();//绑定数据到视图
        initLinshener();


    }


    //数据集合
    private void initData() {

        //tab标签名称集合
        tabNames = new ArrayList();
        //Fragment集合
        fragments = new ArrayList();
        //tablayout数据集合
        tabNames.add(getResources().getString(R.string.article));
        tabNames.add(getResources().getString(R.string.sun));

        // 添加页面
        fragments.add(new Fragment_Comarticle());
        fragments.add(new Fragment_Commsun());

    }


    //绑定数据
    private void setDataToView() {
        VPA = new VpSelectedAdapter(getChildFragmentManager(), tabNames, fragments);
        //设置适配器
        vp.setAdapter(VPA);
        //fragment选中时变换图标
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    comera.setVisibility(View.VISIBLE);
                    community_title_bar_publish_topic.setClickable(false);
                    //相机跳转
                    comera.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            ImageSelectorActivity.start(getActivity(), 9, ImageSelectorActivity.MODE_SINGLE, true, true, true);

                        }
                    });
                } else {
                    comera.setVisibility(View.INVISIBLE);
                    comera.setClickable(false);
                    community_title_bar_publish_topic.setClickable(true);
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //tabLayout和ViewPager绑定
        tablayout.setupWithViewPager(vp);

    }

    //点击监听事件
    private void initLinshener() {
        community_title_bar_publish_topic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), PublishActivity.class);
                startActivity(it);
            }
        });
    }

    public static final int REQUEST_CODE = 1000;

    //判断是否请求权限
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode, grantResults);
    }
    private void doNext(int requestCode, int[] grantResults) {

        if (requestCode == 101) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //用户同意

            }
        } else {
            //用户不同意
            //规避程序崩溃为主的操作
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断当前应用是否已经获得了相关的权限
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //申请相关权限
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
        }
        if (resultCode == RESULT_OK && requestCode == ImageSelectorActivity.REQUEST_IMAGE) {
            ArrayList<String> images = (ArrayList<String>) data.getSerializableExtra(ImageSelectorActivity.REQUEST_OUTPUT);

        }
    }
}
